package com.atguigu.gmallre.mock.service.impl;

import com.atguigu.gmallre.mock.bean.*;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.mapper.*;
import com.atguigu.gmallre.mock.service.*;
import com.atguigu.gmallre.mock.service.adv.impl.AdvServiceImpl;
import com.atguigu.mock.util.RandomNum;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2020-02-25
 */
@Service
@DependsOn(value ={"activityRuleService","activitySkuService"})
public class ActivityInfoServiceImpl extends AdvServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {


    @Autowired
    ActivityRuleService activityRuleService;

    @Autowired
    ActivitySkuService activitySkuService;

    @PostConstruct
    @Override
    public void loadCache(){
        super.loadCache();
        Map<Long, ActivityInfo> activityInfoMap = super.cache;
        Map<Long, ActivityRule> activityRuleMap = activityRuleService.all(true);
        Map<Long, ActivitySku> activitySkuMap = activitySkuService.all(true);
        for (ActivityInfo activityInfo : activityInfoMap.values()) {

            for (ActivitySku activitySku : activitySkuMap.values()) {
                if(activityInfo.getId()==activitySku.getActivityId()){
                    activityInfo.getActivitySkuList().add(activitySku);
                }
            }
            for (ActivityRule ActivityRule : activityRuleMap.values()) {
                if(activityInfo.getId()==ActivityRule.getActivityId()){
                    activityInfo.getActivityRuleList().add(ActivityRule);
                }
            }
        }
    }

    public void updateRecentlyDate(){
        List<ActivityInfo> activityInfoList = list();

        for (ActivityInfo activityInfo : activityInfoList) {

            Date stDate=  DateUtils.addDays(AppConfig.mock_date, 0-(   RandomNum.getRandInt(1,20)));
            Date edDate=  DateUtils.addDays(AppConfig.mock_date,  (   RandomNum.getRandInt(1,20)));



            activityInfo.setStartTime(stDate);
            activityInfo.setCreateTime(stDate);
            activityInfo.setEndTime(edDate);
        }
        saveOrUpdateBatch(activityInfoList) ;
    }


}
